app.register.controller('ComboCtrl', function($scope, $http, $rootScope,
		$route, ComboSer, AppUtil, $routeParams) {
	
	loadFlag($routeParams.id,$scope);
	var myDate = new Date(); 
    function addzero(v) {if (v < 10) return '0' + v;return v.toString();}
	var today = myDate.getFullYear().toString() + '-'+addzero(myDate.getMonth() + 1) + '-' +addzero(myDate.getDate());
	$("#dateStart").val(today);
	$("#dateEnd").val(today);
	// 获取本地存储中的页码页数 tableID+page or size
	var page = AppUtil.GetLocalValue("ComboTable_Page");
	var size = AppUtil.GetLocalValue("ComboTable_Size");
	$scope.pagination = {
			totalItems :0,
		currentPage : page == undefined ? 1 : page,
		itemsPerPage : size == undefined ? 10 : size
	};
	$scope.SearchData = function() {
		$scope.postData = {};
		// 点击查询按钮时,恢复页码为1
		$scope.pagination.currentPage = 1
		if($("#entity_code").val()=="")
		{
			alertMsg("提示","请选择店铺信息.");
			return;
		}
		if($("#dateStart").val()==""||$("#dateEnd").val()=="")
		{
			alertMsg("提示","请选择时间区间.");
			return;
		}
		LoadList();
	};
	// 加载数据方法
	var LoadList = function() {
		
		if ($scope.postData == undefined)
			$scope.postData = {};
		// 表单查询参数
		var params = AppUtil.Params(".form_params");
		params = $.extend({}, params,$scope.selected);
		
		if(!params.entity_code)
			return;
		// 当有查询参数时,重置页数
		
		$scope.postData = $.extend({}, $scope.postData,params);
		$scope.postData.page = $scope.pagination.currentPage;
		$scope.postData.size = $scope.pagination.itemsPerPage;
		// 加载数据
		AppUtil.loading();
		ComboSer.list($scope.postData).success(function(response) {
			AppUtil.remove_loading();
			if(response.code==-1){
				alert(response.message);
			}
			$scope.pagination.totalItems = response.total;
			$scope.datas = response.data;
			$scope.t = response.total;
		}).error(function(response){
			alertMsg("提示",'系统错误,清稍后重试.');
		});
	}

	// 配置分页监听
	$scope.$watch('pagination.refresh', LoadList);

	//加载查询条件
	$http.get("combo/dicts").success(function(data){
		$scope.repairType=data.data.repairType;
	});
	
	// 加载查询信息
	AppUtil.LoadGroups();
	// 导出
	$scope.ExportData = function() {
		if($("#entity_code").val()=="")
		{
			alertMsg("提示","请选择店铺信息.");
			return;
		}
		if($("#dateStart").val()==""||$("#dateEnd").val()=="")
		{
			alertMsg("提示","请选择时间区间.");
			return;
		}
		var data = {};
		data.params=AppUtil.Params(".form_params");
		AppUtil.ExportData(data);
	};


});

app.register.service('ComboSer', function($http, AppUtil) {

	// 查询数据
	this.list = function(postData) {
		AppUtil.loading();
		postData.start = (postData.page - 1 < 0 ? 0 : postData.page - 1)
				* postData.size;
		return $http.post('combo/list', postData).error(function() {
			alertMsg("提示", "系统出错,请稍后重试.");
			AppUtil.remove_loading();
		});
	};

});
